<?php

class Feedback_IndexController extends Zend_Controller_Action
{
    public function init()
    {
        Zend_Registry::get('layout')->setLayout('duty');
        
        $activeResource=Feedback_Model_Feedback::RESOURCE_NAME;
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);

        $this->view->headTitle()->append(Zend_Registry::get('locale')->_('Feedback'));
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
    }
    
    public function indexAction()
    {
        if (!Zend_Registry::get('user')->canPost())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $form=new Feedback_Form_Feedback();
        
        $request=$this->getRequest();
        
        if ($request->isPost())
        {
            if ($form->isValid($request->getPost()))
            {
                Feedback_Model_Feedback::send(
                                            $form->getValue('name'),
                                            $form->getValue('email'),
                                            $form->getValue('message')
                                            );
                
                $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('Message sent'));
                return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
            }
            
            $this->view->message=Zend_Registry::get('locale')->_('Message was not sent');
        }
        
        $this->view->form=$form;
    }
}
